package com.hbnu.collection;

import java.util.Comparator;
import java.util.Set;
import java.util.TreeSet;

/**
 * @author kittychen
 * @since 2025-10-14 14:55
 */
public class TreeSetDemo {
    public static void main(String[] args) {
        Set<Integer> treeSet = new TreeSet<>();

        treeSet.add(6);
        treeSet.add(2);
        treeSet.add(1);
        treeSet.add(3);
        treeSet.add(5);
        treeSet.add(9);
        treeSet.add(7);
        System.out.println(treeSet);

        Set<Person> personSet = new TreeSet<>(new Comparator<Person>() {
            @Override
            public int compare(Person p1, Person p2) {
                if (p1.getAge() > p2.getAge()) {
                    return -1;
                } else if (p1.getAge() < p2.getAge()) {
                    return 1;
                } else {
                    return p1.getName().compareTo(p2.getName());
                }
            }
        });

        personSet.add(new Person("Tom", 20));
        personSet.add(new Person("Jerry", 18));
        System.out.println(personSet);
    }
}
